package com.fast.fast.common.base.page;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Collections;
import java.util.List;

/**
 * 自定义分页对象(V是返回给前端的视图对象)
 *
 * @author lyf
 * @date 2022/01/01 00:00 周六
 **/
@Accessors(chain = true)
@Data
public class PageResult<V> implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @JSONField(ordinal = 1)
    @ApiModelProperty(value = "总条数")
    private Long total;

    @JSONField(ordinal = 2)
    @ApiModelProperty(value = "每页显示条数")
    private Long size;

    @JSONField(ordinal = 3)
    @ApiModelProperty(value = "总页数")
    private Long pages;

    @JSONField(ordinal = 4)
    @ApiModelProperty(value = "页码")
    private Long current;

    @JSONField(ordinal = 5)
    @ApiModelProperty(value = "偏移量")
    private Long offset;

    @JSONField(ordinal = 6)
    @ApiModelProperty(value = "结果集")
    private List<V> records = Collections.emptyList();

    /**
     * 是否存在上一页
     *
     * @return true / false
     */
    public boolean hasPrevious() {
        return this.current > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return current < getPages();
    }

    /**
     * IPage-->自定义分页对象
     *
     * @param iPage
     * @return
     */
    public static PageResult of(IPage iPage) {
        return new PageResult<>()
                .setTotal(iPage.getTotal())
                .setSize(iPage.getSize())
                .setPages(iPage.getPages())
                .setCurrent(iPage.getCurrent())
                .setOffset(iPage.offset())
                .setRecords(iPage.getRecords());
    }
}
